package com.ruoyi.system.service.impl;

import com.ruoyi.common.utils.file.FastDFSUtil;
import com.ruoyi.system.domain.SysCertificate;
import com.ruoyi.system.domain.SysOrganization;
import com.ruoyi.system.mapper.SysCertificateMapper;
import com.ruoyi.system.mapper.SysOrganizationMapper;
import com.ruoyi.system.service.ISysOrganizationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * @Author 柳
 * @Date 2021/2/25
 * @Description TODO
 */
@Service
public class SysOrganizationServiceImpl implements ISysOrganizationService {

    @Autowired
    private SysCertificateMapper sysCertificateMapper;

    @Autowired
    private SysOrganizationMapper sysOrganizationMapper;

    @Autowired
    private FastDFSUtil fastDFSUtil;

    /**
     * 修改机构印章文件路径
     * @param sysOrganization
     */
    @Override
    public void updateSysOrganizationSeal(SysOrganization sysOrganization) {
        String filepath="";
        try {
            //如果用户重新上传印章文件
            if(sysOrganization.getSealfile()!=null){
                //删除原有的印章文件
                SysOrganization Organization = sysOrganizationMapper.querySysOrganizationById(sysOrganization.getOrganizationid());
                fastDFSUtil.deleteFile(Organization.getSeal());
            }
            //获取文件对象
            MultipartFile sealfile = sysOrganization.getSealfile();
            //上传文件
            filepath = fastDFSUtil.uploadFile(sealfile);
            //获取文件全路径
            String url = fastDFSUtil.getResAccessUrl(filepath);
            //封装对象
            sysOrganization.setSeal(url);
        } catch (IOException e) {
            e.printStackTrace();
        }
        sysOrganizationMapper.updateSysOrganizationSeal(sysOrganization);
    }

    /**
     * 修改机构状态为已启用
     * @param organizationid
     */
    @Override
    public void updateSysOrganizationState(Integer organizationid) {
        sysOrganizationMapper.updateSysOrganizationState(organizationid);
    }

    /**
     * 修改机构信息
     * @param sysOrganization
     */
    @Override
    public void updateSysOrganization(SysOrganization sysOrganization) {
//        String filepath="";
//        try {
//            //如果用户重新上传印章文件
//            if(sysOrganization.getSealfile()!=null){
//                //删除原有的印章文件
//                SysOrganization Organization = sysOrganizationMapper.querySysOrganizationById(sysOrganization.getOrganizationid());
//                fastDFSUtil.deleteFile(Organization.getSeal());
//            }
//            //获取文件对象
//            MultipartFile sealfile = sysOrganization.getSealfile();
//            //上传文件
//            filepath = fastDFSUtil.uploadFile(sealfile);
//            //获取文件全路径
//            String url = fastDFSUtil.getResAccessUrl(filepath);
//            //封装对象
//            sysOrganization.setSeal(url);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
        sysOrganizationMapper.updateSysOrganization(sysOrganization);
    }

    /**
     * 删除机构信息
     * @param organizationid
     */
    @Override
    public void deleteSysOrganization(Integer organizationid) {
        //根据id查询机构信息
        SysOrganization sysOrganization = sysOrganizationMapper.querySysOrganizationById(organizationid);
        //删除文件服务器上的印章文件
        fastDFSUtil.deleteFile(sysOrganization.getSeal());
        //删除机构信息
        sysOrganizationMapper.deleteSysOrganization(organizationid);
        //删除机构下的所有证书文件
        List<Integer> ids = sysCertificateMapper.queryCertificateByOrganizationid(organizationid);
        for (Integer id : ids) {
            sysCertificateMapper.deleteSysCertificates(id);
        }
    }

    /**
     * 添加机构信息
     * @param sysOrganization
     */
    @Override
    public void saveSysOrganization(SysOrganization sysOrganization) {
        String filepath="";
        try {
            //获取文件对象
            MultipartFile sealfile = sysOrganization.getSealfile();
            //上传文件
            filepath = fastDFSUtil.uploadFile(sealfile);
            //获取文件全路径
            String url = fastDFSUtil.getResAccessUrl(filepath);
            //封装对象
            sysOrganization.setSeal(url);
        } catch (IOException e) {
            e.printStackTrace();
        }
        sysOrganizationMapper.saveSysOrganization(sysOrganization);
    }

    /**
     * 查询所有机构信息用于填充前端下拉框数据
     * @return
     */
    @Override
    public List<SysOrganization> querySysOrganization(SysOrganization sysOrganization) {
        return sysOrganizationMapper.querySysOrganization(sysOrganization);
    }

    /**
     * 根据id查询机构信息
     * @param organizationid
     * @return
     */
    @Override
    public SysOrganization querySysOrganizationById(Integer organizationid) {
        return sysOrganizationMapper.querySysOrganizationById(organizationid);
    }

     /**
      * 查询机构名称和id
      *
      * @return 对象集合
      * */
     @Override
     public List<SysOrganization> querOrganizationName() {
          List<SysOrganization> sysOrganizations = sysOrganizationMapper.querOrganizationName();
          return sysOrganizations;
     }
}
